package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.AssessmentInfo;
import edu.tms.bean.Evaluating;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 学生评测记录表dao
 * @author Administrator
 *
 */
public class EvaluatingDaoImp implements CommnonDao<Evaluating>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(Evaluating value) throws Exception {
		String sql="insert into t_evaluating value(null,?,?,?,?,?,?,?,?,?,?,?)";
		Evaluating El = (Evaluating)value;
		return JdbcUtil.insertAuto(sql,
				El.getAssessmentId(),//老师的id
				El.getStuId(),//学生得Id
				El.getCreateTime(),//填写评测的时间
				El.getMasterLevel(),
				El.getFeelLevel(),
				El.getAttractionLevel(),
				El.getDutyLevel(),
				El.getAccidentLevel(),
				El.getDisciplineLevel(),
				El.getAggignLevel(),
				El.getAnswerLevel());
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Evaluating value) throws Exception {
		return 0;
	}
	/**
	 * 删除内容
	 * 学生表t_teacher，教师测评表t_assessment_info
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Evaluating value) throws Exception {
			Evaluating el = (Evaluating)value;
			String sql="delete  t_evaluating_info"
			+"from t_teacher "
			+"left join t_course on t_teacher.tch_id=t_course.tch_id "
			+"where tch_id=?";
			return JdbcUtil.update(sql,el.getEvaluatingId());
	}
	/**
	 * 查找所有的内容
	 * 学生表t_teacher，教师测评表t_assessment_info
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Evaluating> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT stu.stu_name,stu.stu_no,e.master_level,e.feel_level,e.attraction_level,e.duty_level,e.accident_level,e.discipline_level,e.aggign_level,e.answer_level ";
				sql+= "FROM t_evaluating AS e ,t_student AS stu WHERE e.stu_id=stu.stu_id LIMIT ?,?";
		return JdbcUtil.query(Evaluating.class,sql,(pageNo-1)*pageSize,pageSize);
	}

	/**
	 * 查找单一实体
	 * 学生表t_teacher，教师测评表t_assessment_info
	 * @param Key
	 * @throws Exception
	 */
	@Override
	public Evaluating findByKey(Object Key) throws Exception{
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Evaluating> findAll(Object Key, int pageNo, int pageSize) throws Exception {
		String sql="select *from t_evaluating as e ,t_student as stu,t_teacher as t "
				+ "where e.stu_id=stu.stu_id and e.assessment_id=t.tch_id"
				+ "or e.assessment_id=? or e.stu_id=? or e.create_time=? or "
				+ "e.master_level=? or e.feel_level=? or e.attraction_level=? or e.duty_level=? or "
				+ "e.accident_level=?"
				+ " limit ?,?";
		return JdbcUtil.query(AssessmentInfo.class,sql,Key,Key,Key,Key,Key,Key,Key,Key,Key,Key,Key,(pageNo-1)*pageSize,pageSize);
	}
	
	
	/**
	 *  不同条件的不同值  查询
	 * @throws Exception 
	 */
	public Long conDiTionalQuery(String a,Integer i) throws Exception{
		String sql="SELECT COUNT("+a+") FROM t_evaluating WHERE "+a+"=? and assessment_id=(SELECT assessment_id FROM t_evaluating ORDER BY assessment_id DESC LIMIT 1)";
		return (Long) JdbcUtil.uniqueValue(sql,1,i);
	}
	
	/**
	 * 查找学生是否已经进行了评测数据
	 * 1表示已经完成存在数据，0表示还未进行数据评测
	 * @throws Exception 
	 */
	public Long selectinfo(Integer stuId) throws Exception{
		String sql="SELECT COUNT(*) FROM  t_evaluating "
				+ "WHERE stu_id=? "
				+ "AND assessment_id="
				+ "(SELECT assessment_id FROM t_assessment_info "
				+ "ORDER BY create_time DESC LIMIT 1)";
		return (Long)JdbcUtil.uniqueValue(sql, 1, stuId);
	}
	
	
	
}
